from __future__ import annotations

from dataclasses import dataclass
from math import tan, radians


@dataclass
class ExcavationStability:
    factor_of_safety: float
    recommend_slope_ratio: float
    note: str


def check_slope_stability(
    slope_height_m: float,
    slope_angle_deg: float,
    cohesion_kPa: float,
    friction_angle_deg: float,
    unit_weight_kN_m3: float = 18.0,
    water_table: bool = False,
) -> ExcavationStability:
    """基坑边坡稳定验算（库仑-朗肯简化）。

    采用极简条分法近似得到安全系数趋势：
    Fs ≈ (c / (γ·H·tanβ)) + (tanφ / tanβ)
    - 若有地下水，取等效γ' ≈ 0.7γ。
    返回建议放坡比 1:m。
    """

    gamma = unit_weight_kN_m3 * (0.7 if water_table else 1.0)
    beta = radians(slope_angle_deg)
    phi = radians(friction_angle_deg)
    numerator = cohesion_kPa / max(1e-6, gamma * slope_height_m * tan(beta)) + tan(phi) / max(1e-6, tan(beta))
    Fs = max(0.1, numerator)
    # 目标 Fs>=1.3，若不足推大放坡
    if Fs < 1.3:
        ratio = max(0.5, (1.3 / Fs)) * 1.0
    else:
        ratio = tan(beta) ** -1
    return ExcavationStability(
        factor_of_safety=Fs,
        recommend_slope_ratio=ratio,
        note="经验近似，深基坑应采用极限平衡/有限元复核",
    )


